#include <iostream>
#include <math.h>
#include <set>

using namespace std;

long long int sumDigFact(long int n);
long long int fact(long int n);


int main()
{

    long long int sum = 0;

    for (int b = 3; b < 100000000 ; b++)
    {

        if ( b == sumDigFact(b))
        {
            sum = sum + b;

cout << b << endl;
        }

    }
    cout << sum;

    return 0;
}




long long int sumDigFact(long int n)
{

    long long int res = 0;

    while (n>0)
    {
        res = res + fact(n % 10);
        n =n /10;
    }
    return res;
}


long long int fact(long int n)
{

    long long int   res = 1;
    while ( n > 0)
    {
        res = res * n;
        n--;

    }

    return res;
}



